package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.PageResult;
import com.heima.mapper.BusinessMapper;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.RegionMapper;
import com.heima.mapper.VmMapper;
import com.heima.pojo.Business;
import com.heima.pojo.Node;
import com.heima.pojo.VmList;
import com.heima.pojo.VmType;
import com.heima.pojo.dto.PageQueryDTO;
import com.heima.pojo.dto.VmPageQureyDTO;
import com.heima.pojo.dto.VmTypeDTO;
import com.heima.pojo.vo.NodeVo;
import com.heima.pojo.vo.RegionVo;
import com.heima.pojo.vo.TypeVo;
import com.heima.pojo.vo.VmListVo;
import com.heima.service.VmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.heima.pojo.VmList.VMSTUATUS_0NT_RELEASED;

@Service
@Slf4j
public class VmServiceImpl implements VmService {

    @Autowired
    private VmMapper vmMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public PageResult vmQuery(VmPageQureyDTO dto) {
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<VmListVo> page = vmMapper.vmQuery(dto);
        //查询自动售货机的类型
        List<TypeVo> typeVo = nodeMapper.typeFindAll();
        //查询点位详细信息
        List<NodeVo> nodeVo = nodeMapper.nodeFindAll();
        //获取所以运营区域的信息
        List<RegionVo> regionVoList = regionMapper.regionALl();
        //获取所有商圈的信息
        List<Business> businessesList = businessMapper.businessAll();

        for (VmListVo vm : page) {
            for (TypeVo type : typeVo) {
                if (vm.getVmType().equals(type.getTypeId())){
                    vm.setType(type);
                }
            }
            for (NodeVo node : nodeVo) {
                for (RegionVo region : regionVoList) {
                    if (node.getRegionId().equals(region.getId())) {
                        node.setRegion(region);
                    }
                }
                for (Business business : businessesList) {
                    if (node.getBusinessId().equals(business.getId())) {
                        node.setBusinessType(business);
                    }
                }
                if (vm.getNodeId().equals(node.getId())){
                    vm.setNode(node);
                }
            }
            for (RegionVo region: regionVoList) {
                if (vm.getRegionId().equals(region.getId())){
                    vm.setRegion(region);
                }
            }
        }
        return new PageResult(dto.getPageIndex(), dto.getPageSize(), page.getPages(), page.getTotal(),page.getResult());
    }

    @Override
    public Boolean save(VmList vmList) {
        vmList.setUpdateTime(LocalDateTime.now());
        vmList.setCreateTime(LocalDateTime.now());
        Node node = nodeMapper.selectById(vmList.getNodeId());
        vmList.setRegionId(node.getRegionId());
        vmList.setBusinessId(node.getBusinessId());
        vmList.setOwnerId(node.getOwnerId());
        vmList.setOwnerName(node.getOwnerName());
        vmList.setVmStatus(VMSTUATUS_0NT_RELEASED);
        int result = vmMapper.insert(vmList);
        return result != 0;
    }

    @Override
    public Boolean update(Long id, Long nodeId) {
        Integer result = vmMapper.updateByIdNode(id,nodeId);
        return result != 0;
    }
}
